package randomwalk;

import java.sql.SQLException;
import java.util.ArrayList;

import evaluation.EvaluationMetric;

import musictrackrecommendation.Dataset;
import musictrackrecommendation.ExperimentParameters;
import randomwalk.socialgraph.SocialGraph;

/**
 * @uml.dependency   supplier="randomwalk.QuizSetVerification"
 */
public class RandomWalkWithRestarts {

	public RandomWalkWithRestarts() {
	}

	public void randomWalkWithRestarts() {
		SocialGraph socialGraph;
		try {
					
			ExperimentParameters parameters = ExperimentParameters.getInstance();
			System.out.println(parameters.getTitle());
			
			socialGraph = parameters.getSocialGraph();
			
			Dataset datasetProperties = parameters.getDatasetProperties();
			
			// id of users
			int[] userIds = parameters.getUsers(); 
			
			// probability of restart random walk on each step
			double restartProbability = parameters.getRestartProbability(); 
			
			EvaluationMetric[] metrics = parameters.getMetrics();
			
			for (int userId : userIds) {
				// predicted preferences of current user
				PreferencesVector preferencesVector = datasetProperties
						.getRestartVector(userId);
				
				QuizSetVerification verification = 
					new QuizSetVerification(userId);

				double[] trackPreferences = socialGraph.randomWalkWithRestarts(
						preferencesVector, restartProbability, verification, userId);

				ArrayList<Integer> topTracks = verification
						.prepareTopTracks(trackPreferences, trackPreferences.length);

				for(EvaluationMetric metric : metrics) {
					metric.addRecommendation(userId, topTracks, verification);
				}
			}

			for(EvaluationMetric metric : metrics) {
				metric.printMeanMetricValue();
			}

		} catch (ClassNotFoundException e) {
			System.out.println("Cannot register JDBC driver");
			e.getMessage();
		} catch (SQLException e) {
			System.out.println("Cannot retrieve data from database");
			e.getMessage();
		}
	}
	
}
